package org.csu.mypetstorems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.csu.mypetstorems.entity.*;
import org.csu.mypetstorems.mapper.CategoryMapper;
import org.csu.mypetstorems.mapper.InventoryMapper;
import org.csu.mypetstorems.mapper.ItemMapper;
import org.csu.mypetstorems.mapper.ProductMapper;
import org.csu.mypetstorems.service.CatalogService;
import org. springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service("catalogService")
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public Boolean addItem(Item item) {
        if(itemMapper.selectById(item.getItemId()) != null) {
            return false;
        }

        Inventory inventory=new Inventory();
        inventory.setItemId(item.getItemId());
        inventory.setQty(10000);
        return (inventoryMapper.insert(inventory)) > 0 && (itemMapper.insert(item)) > 0;
    }

    @Override
    public Boolean updateItem(Item item) {
        if (item.getQty()==0) {
            item.setStatus("SP");
        }
        return itemMapper.updateById(item) > 0;
    }

    @Override
    public Boolean deleteItem(String itemId) {
        return (inventoryMapper.deleteById(itemId)> 0) && (itemMapper.deleteById(itemId) > 0);
    }

    @Override
    public Boolean updateItemStatus(String status, String itemId) {
        return itemMapper.updateItemStatus(status, itemId) > 0;
    }

    @Override
    public PageBean<Item> getItemListWithCondition(Integer pageNum, Integer pageSize, String productIdForSearch, String state) {
        PageBean<Item> pageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Item> itemList = itemMapper.getItemListWithCondition(productIdForSearch, state);
        //怎么实现数据库联合查询 java查询语句
        Page<Item> page = (Page<Item>) itemList;
        pageBean.setTotal(page.getTotal());
        pageBean.setList(page.getResult());
        return pageBean;
    }

    @Override
    public List<String> getItemProductList() {
        return productMapper.selectList(null)
                .stream()
                .map(Product::getProductId)
                .collect(Collectors.toList());
    }

    @Override
    public Boolean addProduct(Product product) {
        if(productMapper.selectById(product.getProductId()) != null) {
            return false;
        }
        return productMapper.insert(product) > 0;
    }

    @Override
    public Boolean updateProduct(Product product) {
        return productMapper.updateById(product) > 0;
    }

    @Override
    public Boolean deleteProduct(String productId) {
        //危险操作
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("productid", productId);
        List<Item> itemList = itemMapper.selectList(queryWrapper);
        for (Item item : itemList) {
            itemMapper.deleteById(item.getItemId());
        }
        return productMapper.deleteById(productId) > 0;
    }

    @Override
    public PageBean<Product> getProductListWithCondition(Integer pageNum, Integer pageSize, String categoryIdForSearch) {
        PageBean<Product> pageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.getProductListWithCondition(categoryIdForSearch);
        Page<Product> page = (Page<Product>) productList;
        pageBean.setTotal(page.getTotal());
        pageBean.setList(page.getResult());
        return pageBean;
    }

    @Override
    public List<String> getproductCategoryList() {
        return categoryMapper.selectList(null)
                .stream()
                .map(Category::getCategoryId)
                .collect(Collectors.toList());
    }

    @Override
    public Boolean addCategory(Category category) {
        if(categoryMapper.selectById(category.getCategoryId()) != null) {
            return false;
        }
        return categoryMapper.insert(category) > 0;
    }

    @Override
    public Boolean updateCategory(Category category) {
        return categoryMapper.updateById(category) > 0;
    }

    @Override
    public Boolean deleteCategory(String categoryId) {
        //这里应该要进行级联删除 而不是使用代码
        QueryWrapper<Product> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("categoryid", categoryId);
        List<Product> productList = productMapper.selectList(queryWrapper1);
        for (Product product : productList) {
            productMapper.deleteById(product.getProductId());
            QueryWrapper<Item> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("productid", product.getProductId());
            List<Item> itemList = itemMapper.selectList(queryWrapper2);
            for (Item item : itemList) {
                itemMapper.deleteById(item.getItemId());
            }
        }
        return categoryMapper.deleteById(categoryId) > 0;
    }

    @Override
    public PageBean<Category> getCategoryListWithCondition(Integer pageNum, Integer pageSize, String categoryIdForSearch) {
        PageBean<Category> pageBean = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<Category> categoryList = categoryMapper.getCategoryListWithCondition(categoryIdForSearch);
        Page<Category> page = (Page<Category>) categoryList;
        pageBean.setTotal(page.getTotal());
        pageBean.setList(page.getResult());
        return pageBean;
    }

}
